unit uStockAdjustment;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, BaseGrid, AdvGrid, ExtCtrls, AdvEdit, AdvCombo,
  RzLabel, RzButton, uDM, DB, MemDS, DBAccess, Uni, RzBckgnd, ComCtrls, AdvEdBtn,
  AdvObj;

type
  TFrmStockAdjustment = class(TForm)
    Panel1: TPanel;
    QtyAkhir: TAdvEdit;
    grid: TAdvStringGrid;
    RzLabel2: TRzLabel;
    RzLabel1: TRzLabel;
    BtnSave: TRzBitBtn;
    BtnCancel: TRzBitBtn;
    BtnEdit: TRzBitBtn;
    BtnDelete: TRzBitBtn;
    RzBitBtn5: TRzBitBtn;
    RzSeparator1: TRzSeparator;
    RzSeparator2: TRzSeparator;
    Tgl: TDateTimePicker;
    Label1: TLabel;
    KodeStock: TAdvEditBtn;
    NamaStock: TAdvEdit;
    QtyFisik: TAdvEdit;
    QtyAdjust: TAdvEdit;
    SatAwal: TAdvEdit;
    SatFisik: TAdvEdit;
    SatAdjust: TAdvEdit;
    LbIndex: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure RoomTypeKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure RzButton5Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RzBitBtn5Click(Sender: TObject);
    procedure BtnEditClick(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure BtnNewClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnDeleteClick(Sender: TObject);
    procedure KodeStockClickBtn(Sender: TObject);
    procedure TglKeyPress(Sender: TObject; var Key: Char);
    procedure KodeStockKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure QtyFisikChange(Sender: TObject);
  private
    { Private declarations }
    procedure initForm;
    Procedure initGrid;
    Procedure LoadData(ID: String);
    Procedure LoadFromData(ID: String);
    procedure setEnableDisable;

    function isValid: boolean;
    function isSaved: boolean;
    function getMaxRoom: integer;
    function calculate: double;
  public
    { Public declarations }
  end;

var
  FrmStockAdjustment: TFrmStockAdjustment;
CONST
  colNO           = 0;
  colID           = 1;
  colTgl          = 2;
  colIDStock      = 3;
  colNamaStock    = 4;
  colQtyAkhir     = 5;
  colQtyFisik     = 6;
  colQtyAdjust    = 7;

implementation

uses uFlash;

{$R *.dfm}

procedure TFrmStockAdjustment.Button1Click(Sender: TObject);
begin
  Close;
end;

function TFrmStockAdjustment.calculate: double;
begin
  Result := QtyFisik.FloatValue - QtyAkhir.FloatValue;
end;

procedure TFrmStockAdjustment.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:= caFree;
end;

procedure TFrmStockAdjustment.FormShow(Sender: TObject);
begin
  LoadData('');
end;

function TFrmStockAdjustment.getMaxRoom: integer;
begin
  OpenSQL(DM.Buffer, 'select count(id_room) from room');
  if DM.Buffer.RecordCount > 0 then
    Result := DM.Buffer.Fields[0].AsInteger
  else
    Result := 0;
end;

procedure TFrmStockAdjustment.initForm;
begin
  LbIndex.Tag := 0;
  Tgl.Date := Now;
  KodeStock.Clear;
  NamaStock.Clear;
  QtyAkhir.Clear;
  QtyFisik.Clear;
  QtyAdjust.Clear;
  SatAwal.Clear;
  SatFisik.Clear;
  SatAdjust.Clear;
  setEnableDisable;
  Tgl.SetFocus;
end;

procedure TFrmStockAdjustment.initGrid;
begin
  grid.AutoSize := True;
  grid.ColWidths[colID] := 0;
end;

function TFrmStockAdjustment.isSaved: boolean;
begin
Result := true;
try
  if LbIndex.Tag = 0 then
    ExecSQL(DM.Buffer, 'insert into stock_adjustment '+
    '(Tanggal, IDStock, QtyAkhir, QtyFisik, QtyAdjust, Satuan) '+
    'values ('+QuotedStr(FormatDateTime('yyyy-mm-dd', Tgl.Date))+','+
    IntToStr(KodeStock.Tag)+','+QtyAkhir.Text+','+
    QtyFisik.Text+','+QtyAdjust.Text+','+QuotedStr(SatAdjust.Text)+')')
  else
    ExecSQL(DM.Buffer, 'update stock_adjustment set '+
    'Tanggal='+QuotedStr(FormatDateTime('yyyy-mm-dd', Tgl.Date))+','+
    'IDStock='+QuotedStr(KodeStock.Text)+','+
    'QtyAkhir='+QuotedStr(QtyAkhir.Text)+','+
    'QtyFisik='+QuotedStr(QtyFisik.Text)+','+
    'QtyAdjust='+QuotedStr(QtyAdjust.Text)+','+
    'satuan='+QuotedStr(QtyAdjust.Text)+
    ' where id='+IntToStr(LbIndex.Tag));
    
  LoadData('');
  Inform(MSG_SAVE_SUCCES);
except
  Alert(MSG_SAVE_UNSUCCES);
  Result:= False;
end;

end;

function TFrmStockAdjustment.isValid: boolean;
begin
  Result:= False;
  if CekInput(Result, KodeStock.Text <> '', 'Kode Stock belum diisi.', KodeStock) then
  if CekInput(Result, QtyAkhir.Text <> '', 'Qty Akhir belum diisi.', QtyAkhir) then
  if CekInput(Result, QtyFisik.Text <> '', 'Qty Fisik belum diisi.', QtyFisik) then
    CekInput(Result, QtyAdjust.Text <> '', 'Qty Adjust belum diisi.', QtyAdjust);

end;

procedure TFrmStockAdjustment.KodeStockClickBtn(Sender: TObject);
begin
  OpenSQL(DM.Lookup, 'select s.IdStock as ID, s.KodeStock as Kode, s.NamaStock as Deskripsi, a.QtyAkhir, s.SatuanStock '+
  'from stock s left join stock_akhir a on (s.IdStock=a.IdStock) and (a.Tanggal=Date(Now())) ');
  DM.Lookup.ReadOnly := True;
  LoadFormLookup;
  KodeStock.Tag := DM.Lookup.Fields[0].AsInteger;
  KodeStock.Text := DM.Lookup.Fields[1].AsString;
  NamaStock.Text := DM.Lookup.Fields[2].AsString;
  QtyAkhir.Text := DM.Lookup.Fields[3].AsString;
  SatAwal.Text := DM.Lookup.Fields[4].AsString;
  SatFisik.Text := DM.Lookup.Fields[4].AsString;
  SatAdjust.Text := DM.Lookup.Fields[4].AsString;
end;

procedure TFrmStockAdjustment.KodeStockKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_f2 then
    KodeStockClickBtn(Sender);
end;

procedure TFrmStockAdjustment.LoadData(ID: String);
var i : integer;
begin
  initForm;

  OpenSQL(DM.Buffer , 'select a.Id,a.Tanggal,s.KodeStock, s.NamaStock, a.QtyAkhir, a.QtyFisik, a.QtyAdjust, s.SatuanStock '+
  'from stock_adjustment a join stock s on a.IDStock=s.IDStock');

  if DM.Buffer.RecordCount > 0 then
  begin
    with grid do
    begin
       for i := 1 to DM.Buffer.RecordCount do
       begin
          RowCount := i + 1;
          Ints[colNO, i] := i;
          Cells[colID, i] := DM.Buffer.Fields[0].AsString;
          Dates[colTgl, i] := DM.Buffer.Fields[1].AsDateTime;
          Cells[colIDStock, i] := DM.Buffer.Fields[2].AsString;
          Cells[colNamaStock, i] := DM.Buffer.Fields[3].AsString;
          Cells[colQtyAkhir, i] := FloatToStrFmt(DM.Buffer.Fields[4].AsInteger, true, 0);
          Cells[colQtyFisik, i] := FloatToStrFmt(DM.Buffer.Fields[5].AsInteger, true, 0);
          Cells[colQtyAdjust, i] := FloatToStrFmt(DM.Buffer.Fields[6].AsInteger, true, 0);
          if i mod 2 = 1 then
              RowColor[i] := clMenu;
          DM.Buffer.Next;
       end;
    end;
  end;
  initGrid;

end;

procedure TFrmStockAdjustment.LoadFromData(ID: String);
begin
  OpenSQL(DM.Buffer , 'select a.Id,a.Tanggal,s.KodeStock, s.NamaStock, a.QtyAkhir, '+
  'a.QtyFisik, a.QtyAdjust, s.SatuanStock '+
  'from stock_adjustment a join stock s on a.KodeStock=s.KodeStock '+
  'where id = '+ID);
  if DM.Buffer.RecordCount > 0 then
  begin
    Tgl.Date := DM.Buffer.Fields[1].AsDateTime;
    KodeStock.Text := DM.Buffer.Fields[2].AsString;
    NamaStock.Text := DM.Buffer.Fields[3].AsString;
    QtyAkhir.Text := DM.Buffer.Fields[4].AsString;
    QtyFisik.Text := DM.Buffer.Fields[5].AsString;
    QtyAdjust.Text := DM.Buffer.Fields[6].AsString;
    SatAwal.Text := DM.Buffer.Fields[7].AsString;
    SatFisik.Text := DM.Buffer.Fields[7].AsString;
    SatAdjust.Text := DM.Buffer.Fields[7].AsString;
    LbIndex.Tag := 1;
  end else
    initForm;
  setEnableDisable;
end;


procedure TFrmStockAdjustment.QtyFisikChange(Sender: TObject);
begin
  QtyAdjust.FloatValue := calculate;
end;

procedure TFrmStockAdjustment.RoomTypeKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_return then
     SelectNext(ActiveControl, true, true);
end;

procedure TFrmStockAdjustment.BtnSaveClick(Sender: TObject);
begin
  if (isValid) //and (getMaxRoom < 181)
   then isSaved;
end;

procedure TFrmStockAdjustment.BtnCancelClick(Sender: TObject);
begin
  initForm;
end;

procedure TFrmStockAdjustment.BtnDeleteClick(Sender: TObject);
begin
  if Confirm(MSG_CONFIRM_DELETE) then
  begin
//    ExecSQL(DM.Buffer, 'delete from room where id_room='+QuotedStr(RoomID.Text));
  end;
end;

procedure TFrmStockAdjustment.BtnEditClick(Sender: TObject);
begin
  LoadFromData(grid.Cells[colID, grid.Row]);
end;

procedure TFrmStockAdjustment.RzBitBtn5Click(Sender: TObject);
begin
  close;
end;

procedure TFrmStockAdjustment.BtnNewClick(Sender: TObject);
begin
  initForm;
end;

procedure TFrmStockAdjustment.RzButton5Click(Sender: TObject);
begin
  close;
end;

procedure TFrmStockAdjustment.setEnableDisable;
begin
  LbIndex.Visible := False;
  BtnEdit.Enabled := (LbIndex.Tag = 0);
  BtnCancel.Enabled := (LbIndex.Tag > 0);
  BtnDelete.Enabled := (LbIndex.Tag > 0);
  QtyAkhir.ReadOnly := True;
  QtyAdjust.ReadOnly := True;
  SatAwal.ReadOnly := True;
  SatFisik.ReadOnly := True;
  SatAdjust.ReadOnly := True;
  NamaStock.ReadOnly := True;
end;

procedure TFrmStockAdjustment.TglKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
    SelectNext(ActiveControl, True, True);
end;

end.
